
#include <BFile>
#include <BLabel>
#include <BHBoxLayout>
#include <BVBoxLayout>
#include <BXmlDocument>

#include "HtmlEditor.h"

HtmlEditor::HtmlEditor(BWidget* parent) : BScrollWidget(parent)
{

}
HtmlEditor::~HtmlEditor()
{

}

void HtmlEditor::open(const BString& fileName)
{
	BFile file(fileName);
	if (!file.open(BFile::Mode_Read))
		return;

	BString title = fileName.name();
	title -= ".html";
	this->setTitle(title);

	BXmlDocument doc;
	BXmlNode* root = doc.read(file);
	if (!root)
		return;

	BXmlNode* body = root->child("body");
	if (!body)
		return;

	this->clearWidgets();

	BWidget* widget = readNode(body);
	this->addWidget(widget);
}
void HtmlEditor::save()
{

}

HtmlBox* HtmlEditor::readNode(BXmlNode* node)
{
	const BString& name = node->name();

	HtmlBox* box = new HtmlBox(node->name());
	box->setName(node->name());

	if (node->text().size())
	{
		BLabel* label = new BLabel(node->text());
		box->addLabel(label);
	}

	for (int i = 0; i < node->childCount(); i++)
	{
		BXmlNode* child = node->child(i);
		const BString& name = child->name();
		HtmlBox* childWidget = readNode(child);
		box->addBox(childWidget);
	}
	return box;
}